<template>
    <div class="content">
        <el-row :gutter="20">
            <el-col :span="12">
                <el-button type="primary" :icon="CirclePlus" @click="onEdit(0)">添加</el-button>
                <el-button type="danger" :icon="Delete">删除</el-button>
            </el-col>
            <el-col :span="12" class="text-right">
                <el-input v-model="form.search" style="width: 240px" placeholder="请输入关键词" />
                <el-button type="primary" class="ml10" :icon="Search" @click="onSearch">搜索</el-button>
                <el-button type="primary" class="ml10" :icon="RefreshRight" @click="onSearch('Refresh')" />
            </el-col>
        </el-row>
        
        <el-table class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
            <el-table-column type="index" :index="indexMethod" label="序号" width="60" />
            <el-table-column prop="name" label="姓名" show-overflow-tooltip />
            <el-table-column prop="mobile" label="手机号码" show-overflow-tooltip />
            <el-table-column label="岗位">
                <template #default="scope">
                    <el-text >{{ scope.row.role_name }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="入场时间">
                <template #default="scope">
                    <el-text>{{ scope.row.pivot.start_date }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="出场时间">
                <template #default="scope">
                    <el-text>{{ scope.row.pivot.end_date }}</el-text>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="100">
                <template #default="scope">
                    <el-button link type="primary" @click="onEdit(scope.row.id)" size="small">编辑</el-button>
                    <el-popconfirm name="确定要删除么?" @confirm="onDelete(scope.row.id)">
                        <template #reference>
                            <el-button link type="danger" size="small">删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script lang="ts" setup>
import { ref,onMounted } from 'vue'
import { CirclePlus, RefreshRight, Search, Delete } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import http from '../../../util/request';
import router from '../../../router';
const indexMethod = (index: number) => {
  return index+= 1
}
const form = ref<any>({search:'',role_type:1,page:0,current:0,total:10,limit:20,loading:false,parentBorder:false})
interface User {
    id: number
    name: string
    mobile: string
    created_at: string
}
const tableData = ref<User[]>([])

const onGetData = () => {
    
    form.value.loading = true
    http.post('/projectUser',form.value).then((response:any)=>{
        form.value.loading = false
        tableData.value = response.data.data
    })
}

const onSearch = (type = '') => {
    if(type == 'Refresh'){
        form.value.search = ''
        onGetData()
        return
    }
    if(!form.value.search){
        ElMessage.error('请输入搜索内容')
    }
    onGetData()
}


const onEdit = (id: number) => {
    router.push({ name: 'userEdit',params: { id: id }})
}

const onDelete = (id: number) => {
    http.post('/deleteBind',{id:id}).then((response:any)=>{
        if(response.data.code != 200){
            ElMessage.error(response.data.msg);return;
        }
        ElMessage.success(response.data.msg);
        onGetData();
    })
}

onMounted(()=>{
    onGetData()
})

</script>

<style scoped>

</style>